<template>
  <div class="app-container-flex">
    <NewFullScreen
      v-model="showDialog"
      :title="title"
      @returnBack="$emit('returnModify')"
    >
      <template v-slot:full-button>
        <el-button @click="save()">保存</el-button>
      </template>
      <div class="model-panel">
        <div class="model-panel-title">
          <p>计量器具信息</p>
        </div>
        <div class="model-panel-body">
          <!-- form -->
          <PageForm
            class="paddingBox order-model-formBox flex_wrap_start_start"
            v-model:ref-obj="formInfo.ref"
            :disabled="formInfo.disabled"
            :data="formInfo.data"
            :field-list="formInfo.fieldList"
            :rules="formInfo.rules"
            :label-width="formInfo.labelWidth"
          >
            <template v-slot:form-commonName>
              <el-input
                v-model="formInfo.data.commonName"
                style="width: 300px"
                class="not-mongolia"
                type="input"
                :required="true"
                :disabled="true"
                :placeholder="'请选择计量器具'"
              >
                <template #append>
                  <el-icon @click="checkProduct"><MoreFilled /></el-icon>
                </template>
              </el-input>
            </template>

            <template v-slot:form-filePathList>
              <UploadImg
                v-model:value="formInfo.data.filePathList"
                :action="action"
                list-type="text"
                title="附件"
                :file-type="fileType"
                :response-fn="handleResponse"
              />
            </template>
          </PageForm>
        </div>
      </div>
      <div class="model-panel">
        <div class="model-panel-title">
          <p>计量结果信息</p>
        </div>
        <div class="model-panel-body">
          <!-- form -->
          <PageForm
            class="paddingBox order-model-formBox flex_wrap_start_start"
            v-model:ref-obj="formInfo.refRes"
            :disabled="formInfo.disabled"
            :data="formInfo.data"
            :field-list="formInfo.fieldListRes"
            :rules="formInfo.rules"
            :label-width="formInfo.labelWidth"
          >
            <template v-slot:form-verificationDate>
              <el-date-picker
                v-model="formInfo.data.verificationDate"
                value-format="YYYY-MM-DD"
                :disabled-date="checkTimePickerOptions.disabledDate"
              />
            </template>

            <template v-slot:form-filePathList>
              <UploadImg
                v-model:value="formInfo.data.filePathList"
                :action="action"
                list-type="text"
                title="附件"
                :file-type="fileType"
                :response-fn="handleResponse"
              />
            </template>
          </PageForm>
        </div>
      </div>
    </NewFullScreen>
    <NewSelectProduct
      v-if="productInfo.visible"
      v-model:show-dialog="productInfo.visible"
      :api="productInfo.api"
      :query="productInfo.query.queryMap"
      :table-key="productInfo.tableKey"
      :title="productInfo.title"
      :highlight-row="true"
      :save-empty="true"
      :check-box="false"
      :allowCoverFilterParams="true"
      @callback="selectProduct"
    />
  </div>
</template>

<script setup name="assetInstrumentsPlanModifg">
import {
  createMeasuringVerificationRecord,
  getMeasuringVerificationRecordById,
  updateMeasuringVerificationRecord,
} from '@/api/quality.js'
import { getMeasuringInstrumentCustomizePage } from '@/api/equipment.js'
import axios from 'axios'
const { proxy } = getCurrentInstance()
const props = defineProps(['visible', 'title', 'planId'])
//表单相关
const formRefs = ref(null)
const checkTimePickerOptions = ref({
  disabledDate: (time) => {
    return (
      time.getTime() <
      new Date(formInfo.value.data.preVerificationDate).getTime()
    )
  },
})
const formInfo = ref({
  ref: null,
  refRes: null,
  data: {},
  fieldList: [
    {
      label: '器具名称',
      value: 'commonName',
      type: 'slot',
      disabled: true,
    },
    { label: '器具编码', value: 'code', type: 'input', disabled: true },
    {
      label: '是否强检',
      value: 'isMandatoryInspectionValue',
      type: 'select-model',
      code: 'yes_or_no',
      disabled: true,
    },
    {
      label: '资产名称',
      value: 'assetName',
      type: 'input',
      disabled: true,
    },
    {
      label: '资产卡片号',
      value: 'assetCode',
      type: 'input',
      disabled: true,
    },
    {
      label: '规格',
      value: 'standard',
      type: 'input',
      disabled: true,
    },
  ],
  fieldListRes: [
    {
      label: '检定日期',
      value: 'verificationDate',
      type: 'slot',
      placeholder: false,
    },
    {
      label: '检定证书编号',
      value: 'verificationCertificateNumber',
      type: 'input',
      maxlength: 32,
    },
    {
      label: '检定类型',
      value: 'verificationType',
      type: 'select-model',
      code: 'verification_type',
      checkDefault: true,
    },
    {
      label: '检定结果',
      value: 'verificationResult',
      type: 'select-model',
      code: 'verification_result',
      checkDefault: true,
    },
    {
      label: '检定单位',
      value: 'verificationUnit',
      type: 'input',
      maxlength: 200,
    },
    {
      label: '上传附件',
      value: 'filePathList',
      type: 'slot',
      className: 'not-br',
    },
  ],
  rules: {
    commonName: [
      { required: true, message: '请选择计量器具', trigger: 'change' },
    ],
    verificationDate: [
      { required: true, message: '请选择检定日期', trigger: 'change' },
    ],
    verificationCertificateNumber: [
      { required: true, message: '请输入证书编号', trigger: 'blur' },
    ],
    verificationUnit: [
      { required: true, message: '请输入检定单位', trigger: 'blur' },
    ],
  },
  labelWidth: '120px',
})
const productInfo = ref({
  title: '选择计量器具',
  visible: false,
  row: {},
  data: {},
  key: '',
  tableKey:
    'measuringRecordQuerymeasuringList-com.slip.spd.entity.vo.asset.settings.MeasuringInstrumentVO',
  api: getMeasuringInstrumentCustomizePage,
  query: {
    queryMap: {
      status: {
        queryType: 'IN',
        value: '1',
      },
      enableStatus: {
        queryType: 'IN',
        value: '1',
      },
      type: {
        asTbName: 'core_product_new',
        columnName: '`type`',
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: '3',
      },
      id: {
        queryType: 'NOT_IN',
        queryCategory: 'AND',
        value: '',
      },
    },
  },
})
const action = ref(axios.defaults.baseURL + '/pub/uploadfile')
const fileType = ref(['docx', 'pdf', 'doc'])
const showDialog = computed({
  get() {
    return props.visible
  },
  set(val) {
    proxy.$emit('update:visible', val) // 更新父组件visible
  },
})
function checkProduct(row, index) {
  productInfo.value.visible = true
}
function selectProduct(row) {
  let type = Object.prototype.toString.call(row)
  row =
    type === '[object Object]' ? [row] : type === '[object Array]' ? row : []
  if (row.length) {
    productInfo.value.query.queryMap.id.value = row[0].id
    productInfo.value.row = row[0]
    formInfo.value.data.commonName = row[0].commonName
    formInfo.value.data.code = row[0].code
    formInfo.value.data.isMandatoryInspectionValue =
      row[0].isMandatoryInspectionValue
    formInfo.value.data.assetName = row[0].assetName
    formInfo.value.data.assetCode = row[0].assetCode
    formInfo.value.data.standard = row[0].standard
    formInfo.value.data.measuringInstrumentId = row[0].id
  } else {
    formInfo.value.data.commonName = ''
    formInfo.value.data.code = ''
    formInfo.value.data.isMandatoryInspectionValue = ''
    formInfo.value.data.assetName = ''
    formInfo.value.data.assetCode = ''
    formInfo.value.data.standard = ''
    productInfo.value.query.queryMap.id.value = ''
    productInfo.value.row = {}
  }
}
//保存
function save() {
  proxy.$fn.checkForm(formRefs.value).then(() => {
    let data = formInfo.value.data
    let api = props.planId
      ? updateMeasuringVerificationRecord
      : createMeasuringVerificationRecord
    api(data).then((resp) => {
      if (resp && resp.success) {
        showDialog.value = false
        formInfo.value.ref.validateField()
        proxy.$emit('returnModify', false)
      }
    })
  })
}
//选择供应商

function getMain() {
  getMeasuringVerificationRecordById(props.planId).then((resp) => {
    if (resp && resp.success) {
      formInfo.value.data = resp.data
    }
  })
}
function handleResponse(response, file) {
  return file
}
onMounted(() => {
  if (props.planId) {
    getMain()
  }
  formRefs.value = [formInfo.value.ref, formInfo.value.refRes]
})
</script>

<style scoped></style>
